package com.blue.http.invoke;

import javax.annotation.Resource;

import com.blue.http.annotation.HttpFilter;
import com.blue.http.config.AuthConfig;
import com.blue.http.config.ErrorConfig;
import com.blue.http.invoke.InvokerFilter;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.HttpResponse;
import com.blue.http.util.HttpException;

/**
 * 数据验证过滤器
 * 
 * @author zhengj
 * @since 1.0 2016年10月11日
 */
@HttpFilter(value = "/*", exclude = "/token")
public class TokenInvokerFilter implements InvokerFilter
{
	@Resource
	private AuthConfig authConfig;
	private ErrorConfig errorConfig = ErrorConfig.getInstance();

	public TokenInvokerFilter()
	{
	}

	@Override
	public boolean preHandle(HttpRequest request, HttpResponse response) throws HttpException
	{
		String token = request.getQueryString("token");
		if (token == null || token.isEmpty())
		{
			response.setCode(ErrorConfig.PARAM);
			response.setMsg(errorConfig.get(response.getCode()));
			response.setExtmsg("令牌不能为空");
			return false;
		}
		boolean auth = authConfig.auth(token, request.getUrl());
		if (!auth)
		{
			response.setCode(ErrorConfig.AUTH);
			response.setMsg(errorConfig.get(response.getCode()));
		}

		return auth;
	}

	@Override
	public void afterHandle(HttpRequest request, HttpResponse response)
			throws HttpException
	{

	}

}
